#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data time : 2022/9/16   6:31 下午
# project   : func.py
# IDE       : PyCharm
# email     :17600960626@163.com

import _ctypes
import os
import re
import threading

from filelock import FileLock
from lib.loggers import log
import time


def think_time(func):
    def thinktime(*args, **kwargs):
        time.sleep(1)
        return func(*args, **kwargs)

    return thinktime

#线程加锁
class GetDevice(object):
    _instance_lock = threading.Lock()

    def __init__(self):
        time.sleep(0.5)

    @classmethod
    #检查是否存在device属性
    def instance(cls, *args, **kwargs):
        if not hasattr(GetDevice, "_device"):
            GetDevice._device = str(*args)

        return _ctypes.PyObj_FromPtr(int(GetDevice._device))

    def _device_lock(self):
        fn = "./drivers/devices.json"
        lock = FileLock(fn + ".lock")
        with lock:
            if os.path.isfile(fn):
                open(fn, ).read()
            else:
                driver = [1, 2, 3]
                driver_s = str(id(driver[0]))
                open(fn, 'a').write(driver_s)
